package com.sba.core.service.impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.sba.core.domain.Region;
import com.sba.core.repository.RegionRepository;
import com.sba.core.service.RegionService;
import com.sba.core.util.StringUtil;

/**
 * 
 * @author feiliming
 *
 */
@Service
public class RegionServiceImpl extends BaseServiceImpl<Region> implements RegionService{

	//private static final Logger log = LoggerFactory.getLogger(RegionServiceImpl.class);
	
	@Autowired
	RegionRepository regionRepository;
	
	@Override
	public Region getById(Long id){
		Region region = regionRepository.findOne(id);
		if(region.getParentId() != null && region.getParentId() != 0){
			Region pregion = regionRepository.findOne(region.getParentId());
			region.setParentName(pregion.getName());
		}else{
			region.setParentName("");
		}
		return region;
	}
	
	@Override
	public Page<Region> getRegions(int limit, int offset) {
		return regionRepository.findByIsDelete(0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Region> getRegionsByNameLike(String name, int limit, int offset) {
		return regionRepository.findByNameLikeAndIsDelete("%" + name + "%", 0, buildPageRequest(limit, offset));
	}

	@Override
	public Page<Region> getRegionsByParentId(Long parentId, int limit, int offset) {
		return regionRepository.findByParentIdAndIsDelete(parentId, 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Region> getRegionsByparentIdAndNameLike(Long parentId, String name, int limit, int offset) {
		return regionRepository.findByParentIdAndNameLikeAndIsDelete(parentId, "%" + name + "%", 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Region> getRegionsByRegionType(String regionType, int limit, int offset) {
		return regionRepository.findByRegionTypeAndIsDelete(regionType, 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Region> getRegionsByRegionTypeAndNameLike(String regionType, String name, int limit, int offset) {
		return regionRepository.findByRegionTypeAndNameLikeAndIsDelete(regionType, "%" + name + "%", 0, buildPageRequest(limit, offset));
	}
	
	private PageRequest buildPageRequest(int limit, int offset){
		Order typeOrder = new Order(Direction.ASC, "regionType");
		Order sortOrder = new Order(Direction.ASC, "sort");
		Order nameOrder = new Order(Direction.ASC, "name");
		Sort sort = new Sort(typeOrder, sortOrder, nameOrder);
		int pageNumber = (offset + limit)/limit;
		int pageSize = limit;
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);
		return pageRequest;
	}

	//子节点递归删除
	@Override
	public void delete(String ids) {
		String[] sids = ids.split(",");
		Set<Region> regions = new HashSet<Region>();
		for(String sid : sids){
			if(StringUtil.isBlank(sid)){
				continue;
			}
			Region region = new Region();
			region.setId(Long.parseLong(sid));
			regions.add(region);
		}
		regionRepository.delete(regions);
	}

}
